package com.ruoyi.ruoyiyinzheng.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.ruoyiyinzheng.domain.vo.StExperimentResultVo;
import com.ruoyi.ruoyiyinzheng.domain.bo.StExperimentResultBo;
import com.ruoyi.ruoyiyinzheng.service.IStExperimentResultService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 实验结果控制器
 * 前端访问路由地址为:/ruoyiyinzheng/experimentResult
 *
 * @author ruoyi
 * @date 2023-09-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/experimentResult")
public class StExperimentResultController extends BaseController {

    private final IStExperimentResultService iStExperimentResultService;

    /**
     * 查询实验结果列表
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:list")
    @GetMapping("/list")
    public TableDataInfo<StExperimentResultVo> list(StExperimentResultBo bo, PageQuery pageQuery) {
        return iStExperimentResultService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出实验结果列表
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:export")
    @Log(title = "实验结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StExperimentResultBo bo, HttpServletResponse response) {
        List<StExperimentResultVo> list = iStExperimentResultService.queryList(bo);
        ExcelUtil.exportExcel(list, "实验结果", StExperimentResultVo.class, response);
    }

    /**
     * 获取实验结果详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:query")
    @GetMapping("/{id}")
    public R<StExperimentResultVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iStExperimentResultService.queryById(id));
    }

    /**
     * 新增实验结果
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:add")
    @Log(title = "实验结果", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StExperimentResultBo bo) {
        if(iStExperimentResultService.selectExperimentNumberCount(Long.valueOf(bo.getExperimentNumber()))>0)
        {
         return    R.fail(StringUtils.format("实验编号：{}存在",bo.getExperimentNumber()));

        }

        return toAjax(iStExperimentResultService.insertByBo(bo));
    }

    /**
     * 修改实验结果
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:edit")
    @Log(title = "实验结果", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StExperimentResultBo bo) {
        return toAjax(iStExperimentResultService.updateByBo(bo));
    }

    /**
     * 删除实验结果
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ruoyiyinzheng:experimentResult:remove")
    @Log(title = "实验结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iStExperimentResultService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


//    @SaCheckPermission("ruoyiyinzheng:experimentResult:list")
//    @GetMapping(value = "/count/{Id}")
//    public R<Integer> count(@PathVariable("Id") Long Id) {
//        return R.ok(iStExperimentResultService.selectExperimentNumberCount(Id));
//    }
}
